/**
 * RemoteRegistrationInfo.java
 */
package fss.common.communication;

import java.io.Serializable;
import java.rmi.RemoteException;

import fss.remote.RemoteClient;
import fss.remote.RemoteService;
import fss.remote.ServiceRegistry;
import fss.util.rmi.RmiId;

/**
 * The registration data structure for various remote objects.
 * 
 * @author thinh
 *
 */
public class RemoteRegistrationInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * The remote identifier.
     */
    private RmiId registeredId = null;
    /**
     * The remote type.
     */
    private String remoteType = null;
    /**
     * Registration information for a remote service.
     * @param service the remote service.
     * @throws RemoteException error building this registration informatoin.
     */
    public RemoteRegistrationInfo(RemoteService service) throws RemoteException {
        if(service == null) throw new RemoteException("Regisration for a null Remote Service cannot be instantiated.");
        this.registeredId = service.getId();
        this.remoteType = service.getClass().getName();
    }
    /**
     * Registration information for a service registry.
     * @param registry the service registry.
     * @throws RemoteException error building this registration informatoin.
     */
    public RemoteRegistrationInfo(ServiceRegistry registry) throws RemoteException {
        if(registry == null) throw new RemoteException("Regisration for a null Service Registry cannot be instantiated.");
        this.registeredId = registry.getId();
        this.remoteType = registry.getClass().toString();
    }
    /**
     * Registration information for a remote client.
     * @param registry the remote client.
     * @throws RemoteException error building this registration informatoin.
     */
    public RemoteRegistrationInfo(RemoteClient client) throws RemoteException {
        if(client == null) throw new RemoteException("Regisration for a null Remote Client cannot be instantiated.");
        this.registeredId = client.getId();
        this.remoteType = client.getClass().toString();
    }
    /**
     * Get the remote object type (i.e. the class name).
     * @return the type.
     */
    public String getRemoteType() {
        return remoteType;
    }
    /**
     * Set the remote object type.
     * @param type the value.
     */
    public void setRemoteType(String type) {
        this.remoteType = type;
    }
    /**
     * Get the remote identifier.
     * @return the id.
     */
    public RmiId getRegisteredId() {
        return registeredId;
    }
    /**
     * Set the remote identifier.
     * @param id the value.
     */
    public void setRegisteredId(RmiId id) {
        this.registeredId = id;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((registeredId == null) ? 0 : registeredId.hashCode());
        result = prime * result + ((remoteType == null) ? 0 : remoteType.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RemoteRegistrationInfo other = (RemoteRegistrationInfo) obj;
        if (registeredId == null) {
            if (other.registeredId != null)
                return false;
        } else if (!registeredId.equals(other.registeredId))
            return false;
        if (remoteType == null) {
            if (other.remoteType != null)
                return false;
        } else if (!remoteType.equals(other.remoteType))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "" + registeredId + "(" + remoteType + ")";
    }
}
